package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.CustomPopupWindow;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.SecondHandItemClickCall;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.Glide4Engine;
import com.kmproject.haifu.app.utils.StringUtils;
import com.kmproject.haifu.app.utils.TimeUtils;
import com.kmproject.haifu.di.component.DaggerSecondHandPublicshComponent;
import com.kmproject.haifu.mvp.contract.SecondHandPublicshContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.MessageEvent;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandTreeTypeBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.SecondHandSaveRequest;
import com.kmproject.haifu.mvp.model.entity.resbean.SelectAddressMapBean;
import com.kmproject.haifu.mvp.presenter.HousePublishDuanZuPublishPresenter;
import com.kmproject.haifu.mvp.presenter.SecondHandPublicshPresenter;
import com.kmproject.haifu.mvp.ui.adapter.SecondHandPublicshBqAdapter;
import com.kmproject.haifu.mvp.ui.adapter.SecondHandPublishAddImgAdapter;
import com.kmproject.haifu.mvp.ui.widget.MultiSelectPopWindow;
import com.kmproject.haifu.mvp.ui.widget.MultiSelectPopWindow1;
import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xw.repo.XEditText;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/27/2019 13:44
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class SecondHandPublicshActivity extends BaseActivity<SecondHandPublicshPresenter> implements SecondHandPublicshContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.secondHandPublicsh_conten_bt_txt)
    XEditText secondHandPublicshContenBtTxt;
    @BindView(R.id.secondHandPublicsh_conten_ms_txt)
    EditText secondHandPublicshContenMsTxt;
    @BindView(R.id.secondHandPublicsh_conten_num_txt)
    TextView secondHandPublicshContenNumTxt;
    @BindView(R.id.secondHandPublicsh_img_RecyclerView)
    RecyclerView secondHandPublicshImgRecyclerView;
    @BindView(R.id.secondHandPublicsh_conten_lf_txt)
    TextView secondHandPublicshContenLfTxt;
    @BindView(R.id.secondHandPublicsh_conten_jg_txt)
    TextView secondHandPublicshContenJgTxt;
    @BindView(R.id.secondHandPublicsh_conten_jtdz_txt)
    EditText secondHandPublicshContenJtdzTxt;
    @BindView(R.id.secondHandPublicsh_conten_RecyclerView)
    RecyclerView secondHandPublicshContenRecyclerView;
    @BindView(R.id.secondHandPublicsh_conten_fbfs)
    TextView secondHandPublicshContenFbfs;
    @BindView(R.id.secondHandPublicsh_conten_dpgs_txt)
    TextView secondHandPublicshContenDpgsTxt;
    @BindView(R.id.secondHandPublicsh_conten_dpgs_lay)
    LinearLayout secondHandPublicshContenDpgsLay;
    @BindView(R.id.Mystory_addStory_PhoneSsqy_txt)
    TextView MystoryAddStoryPhoneSsqyTxt;
    @BindView(R.id.Mystory_addStory_lxdh)
    EditText MystoryAddStoryLxdh;
    @BindView(R.id.housepublishpublish_conten_wxh_txt)
    EditText housepublishpublishContenWxhTxt;
    @BindView(R.id.secondHandPublicsh_next_txt)
    TextView secondHandPublicshNextTxt;
    @BindView(R.id.secondHandPublicsh_next_lay)
    LinearLayout secondHandPublicshNextLay;
    List<MyStoryBean.ResultBean> mresultBeans;//我的在线的店铺

    @BindView(R.id.secondHandPublicsh_conten_dpgs_view)
    View secondHandPublicshContenDpgsView;
    @BindView(R.id.secondHandPublicsh_conten_qy_txt)
    TextView secondHandPublicshContenQyTxt;
    List<HousePayWayBean> housePayWayBeansBq;
    List<SecondHandTreeTypeBean> msecondHandTreeTypeBeans;
    SecondHandPublicshBqAdapter secondHandPublicshBqAdapter;
    RxPermissions mRxPermissions;

    private SecondHandPublishAddImgAdapter addImgAdapter;
    List<SecondHandInfoBean.ImagesBean> images = new ArrayList<>();

     List<HousePayWayBean> houseArea;//区域
    SecondHandSaveRequest secondHandSaveRequest = new SecondHandSaveRequest();
    HousePayWayBean housePayWayBeanPartent,housePayWayBeanChild;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSecondHandPublicshComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_second_hand_publicsh; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        secondHandSaveRequest.setDeliveryAddress("测试地址");
        secondHandSaveRequest.setLgt("102.565");
        secondHandSaveRequest.setLat("25.521");


        UserBean userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);
        if (userBean != null && userBean.getMobile() != null && userBean.getMobile().length() > 2) {
            MystoryAddStoryLxdh.setText(userBean.getMobile().substring(2, userBean.getMobile().length()));
            if (MystoryAddStoryLxdh.getText().toString().length() > 1 && "1".equals(MystoryAddStoryLxdh.getText().toString().substring(0, 1))) {
                MystoryAddStoryPhoneSsqyTxt.setText("+86");
            } else {
                MystoryAddStoryPhoneSsqyTxt.setText("+63");
            }
        }
        secondHandPublicshContenJtdzTxt.setFocusable(false);
        mRxPermissions = new RxPermissions(this);
        mPresenter.getHouseArea(Api.HOUSE_AREA, houseArea);
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_EDEDED));
        mPresenter.getHousePayWayBean(Api.SECOND_HAND_LABEL, "", housePayWayBeansBq);
        mPresenter.secondHandCategoryTree(msecondHandTreeTypeBeans);
        secondHandPublicshContenRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        secondHandPublicshBqAdapter = new SecondHandPublicshBqAdapter(new ArrayList<>());
        secondHandPublicshContenRecyclerView.setAdapter(secondHandPublicshBqAdapter);
        mPresenter.querySelectStory();//查询店铺
        initView();
        if (getIntent().hasExtra("secondHandId")){
            //编辑
            mPresenter.secondHandLookOne(getIntent().getStringExtra("secondHandId"));
        }
    }

    private void initView() {
        secondHandPublicshImgRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        images.add(new SecondHandInfoBean.ImagesBean());
        addImgAdapter = new SecondHandPublishAddImgAdapter(images, mPresenter);
        secondHandPublicshImgRecyclerView.setAdapter(addImgAdapter);
        secondHandPublicshImgRecyclerView.addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(2)));
        addImgAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId()==R.id.add_img_item_del)
                {
                    showDelDialog(position);
                }

            }
        });
        secondHandPublicshContenMsTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null || s.length() > 0) {
                    if (s.length() > 1000) {
                        secondHandPublicshContenNumTxt.setText("1000/1000");

                    } else {
                        secondHandPublicshContenNumTxt.setText(s.length() + "/1000");
                    }
                }
            }
        });

    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void dictListSuccess(String value, List<HousePayWayBean> housePayWayBeans) {
        switch (value) {
            case Api.SECOND_HAND_LABEL:
                housePayWayBeansBq = housePayWayBeans;
                secondHandPublicshImgRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        secondHandPublicshBqAdapter.setNewData(housePayWayBeansBq);
                    }
                }, 500);

                break;
            case Api.HOUSE_AREA:
                this.houseArea = housePayWayBeans;

                break;

        }
    }

    @Override
    public void querySelectStorySuccess(List<MyStoryBean.ResultBean> resultBeans) {
        mresultBeans = resultBeans;
        if (resultBeans == null || resultBeans.isEmpty()) {
            //没有店铺,只能发布个人的
            secondHandPublicshContenFbfs.setText("个人发布");
            secondHandPublicshContenFbfs.setEnabled(false);
            secondHandSaveRequest.setPublishWay(0);
            return;
        }
        if (mresultBeans == null || mresultBeans.isEmpty() || mresultBeans.size() == 1) {
            secondHandPublicshContenDpgsLay.setVisibility(View.GONE);
            secondHandPublicshContenDpgsView.setVisibility(View.VISIBLE);
        } else {
//            secondHandPublicshContenDpgsLay.setVisibility(View.VISIBLE);
//            secondHandPublicshContenDpgsView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void secondHandCategoryTreeSuccess(List<SecondHandTreeTypeBean> secondHandTreeTypeBeans) {
        msecondHandTreeTypeBeans = secondHandTreeTypeBeans;

    }

    @Override
    public RxPermissions getPermissions() {
        return mRxPermissions;
    }

    @Override
    public void requestPerimissionSuccess() {
        //拍照界面
        Matisse.from(AppManager.getAppManager().getCurrentActivity())
                .choose(MimeType.ofImage())
                .countable(true)
                .capture(true)
                .captureStrategy(
//                new CaptureStrategy(true, "com.lxc.inspectionmanagement.fileprovider"))
                        new CaptureStrategy(true, "com.kmproject.haifu.fileprovider"))
                .maxSelectable(9 - (images.size() - 1))
                .gridExpectedSize(AppManager.getAppManager().getCurrentActivity().getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f)
                .imageEngine(new Glide4Engine())
                .forResult(AppConstant.REQUEST_CODE_CHOOSE);
    }

    @Override
    public void updateImgSuccess(String sercerPath) {

        SecondHandInfoBean.ImagesBean houseImageListBean = new SecondHandInfoBean.ImagesBean(TimeUtils.date2String(new Date()), images.size() - 1,
                sercerPath.substring(sercerPath.lastIndexOf(".") + 1, sercerPath.length()), sercerPath, TimeUtils.date2String(new Date()));
        images.remove(images.size() - 1);
        images.add(houseImageListBean);
        images.add(new SecondHandInfoBean.ImagesBean());
        secondHandPublicshImgRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismiss();
                addImgAdapter.setNewData(images);
            }
        });

    }

    @Override
    public void updateImgFail() {

    }

    @Override
    public void secondHandAddSuccess(ResultBean<SecondHandInfoBean> secondHandInfoBeanResultBean) {
   mPresenter.popuwindPublishSuccess(this,secondHandPublicshContenJgTxt,secondHandInfoBeanResultBean.getMessage());
    }

    @Override
    public void secondHandEditSuccess(ResultBean<SecondHandInfoBean> secondHandInfoBeanResultBean) {
        secondHandPublicshContenJtdzTxt.postDelayed(new Runnable() {
            @Override
            public void run() {
                showMessage("编辑成功");
                killMyself();
            }
        },500);
    }

    @Override
    public void secondHandLookOneSuccess(SecondHandInfoBean secondHandInfoBean) {
        secondHandPublicshContenJtdzTxt.setFocusableInTouchMode(true);
        secondHandPublicshContenJtdzTxt.setFocusable(true);
        secondHandPublicshContenJtdzTxt.requestFocus();
        secondHandPublicshContenJtdzTxt.setText(secondHandInfoBean.getDeliveryAddress());
        secondHandSaveRequest.setDeliveryAddress(secondHandInfoBean.getDeliveryAddress());
        secondHandSaveRequest.setLgt(secondHandInfoBean.getLgt());
        secondHandSaveRequest.setLat(secondHandInfoBean.getLat());

        secondHandPublicshContenBtTxt.setText(secondHandInfoBean.getTitle());
        secondHandSaveRequest.setTitle(secondHandPublicshContenBtTxt.getText().toString());
        images=secondHandInfoBean.getImages();
        images.add(new SecondHandInfoBean.ImagesBean());
         addImgAdapter.setNewData(images);
        secondHandSaveRequest.setPublishWay(secondHandInfoBean.getPublishWay());
         if (secondHandInfoBean.getPublishWay()==0)
         {
             secondHandPublicshContenFbfs.setText("个人发布");
             secondHandPublicshContenDpgsLay.setVisibility(View.GONE);
             secondHandPublicshContenDpgsView.setVisibility(View.GONE);
         }else {
             secondHandPublicshContenDpgsLay.setVisibility(View.VISIBLE);
             secondHandPublicshContenDpgsView.setVisibility(View.VISIBLE);
             secondHandPublicshContenFbfs.setText("店铺发布");
             secondHandSaveRequest.setUserStoreId(Long.parseLong(secondHandInfoBean.getUserStoreId()));
             secondHandPublicshContenDpgsTxt.setText(secondHandInfoBean.getStoreInfo().getStoreName());
         }
        secondHandSaveRequest.setSecondHandCategoryId(Long.parseLong(secondHandInfoBean.getSecondHandCategoryId()));
        secondHandPublicshContenLfTxt.setText(secondHandInfoBean.getSecondHandCategoryId());//选择列表名字
        secondHandSaveRequest.setSalePrice(secondHandInfoBean.getSalePrice());
        secondHandSaveRequest.setBuyPrice(secondHandInfoBean.getBuyPrice());

        secondHandSaveRequest.setAreaId(Long.parseLong(secondHandInfoBean.getAreaId()));
        housePayWayBeanPartent=new HousePayWayBean();
        housePayWayBeanPartent.setDictId(secondHandInfoBean.getAreaInfo().get(0).getAreaId()+"");
        housePayWayBeanPartent.setName(secondHandInfoBean.getAreaInfo().get(0).getName());
        housePayWayBeanPartent.setLat(secondHandInfoBean.getLat());
        housePayWayBeanPartent.setLgt(secondHandInfoBean.getLgt());
        secondHandPublicshContenQyTxt.setText(secondHandInfoBean.getAreaInfo().get(0).getName()+secondHandInfoBean.getAreaInfo().get(1).getName());
        housePayWayBeanChild=new HousePayWayBean();
        housePayWayBeanChild.setDictId(secondHandInfoBean.getAreaInfo().get(1).getAreaId()+"");
        housePayWayBeanChild.setName(secondHandInfoBean.getAreaInfo().get(1).getName());
        housePayWayBeanChild.setLat(secondHandInfoBean.getLat());
        housePayWayBeanChild.setLgt(secondHandInfoBean.getLgt());
        if (secondHandInfoBean.getMobile().substring(0,2).equals("86"))
        {
            MystoryAddStoryPhoneSsqyTxt.setText("+86");
        }else {
            MystoryAddStoryPhoneSsqyTxt.setText("+63");
        }
        MystoryAddStoryLxdh.setText(secondHandInfoBean.getMobile().substring(2,secondHandInfoBean.getMobile().length()));
        String ids="";
        if (secondHandInfoBean.getLabels()!=null&&!secondHandInfoBean.getLabels().isEmpty())
        {
            for (int i=0;i<secondHandInfoBean.getLabels().size();i++)
            {
                if (TextUtils.isEmpty(ids))
                {
                    ids+=secondHandInfoBean.getLabels().get(i).getDictId();
                }else {
                    ids+=(","+secondHandInfoBean.getLabels().get(i).getDictId());
                }
            }
            secondHandPublicshBqAdapter.setSelectIds(ids);
            secondHandPublicshBqAdapter.notifyDataSetChanged();
            }

        housepublishpublishContenWxhTxt.setText(secondHandInfoBean.getWechat());
        secondHandPublicshContenMsTxt.setText(secondHandInfoBean.getDetail());

    }

    /**
     * 删除图片
     *
     * @param delindex
     */
    public void showDelDialog(final int delindex) {
        new AlertView("", "确定删除？", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

            @Override
            public void onItemClick(Object o, int position) {
                if (position == AlertView.CANCELPOSITION) {
                    //取消
                } else {
                    images.remove(delindex);
                    secondHandPublicshImgRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            showMessage("删除成功");
                            addImgAdapter.setNewData(images);
                        }
                    });

                }
            }
        }).show();
    }


    public void YsImg(final List<Uri> temmSelected) {

        if (temmSelected != null && !temmSelected.isEmpty()) {
            loadingDialog.show();
            for (int i = 0; i < temmSelected.size(); i++) {

                AppUtil.getBitmapFromUri(this, temmSelected.get(i));
                String path = AppUtil.getFromMediaUri(this, getContentResolver(), temmSelected.get(i));
                if (!TextUtils.isEmpty(path)) {
                    File file = new File(path);
                    if (file != null && file.exists()) {
                        Luban.with(this)
                                .load(file)
                                .ignoreBy(100)
                                //                                .setTargetDir()
                                .filter(new CompressionPredicate() {
                                    @Override
                                    public boolean apply(String path) {
                                        return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
                                    }
                                })
                                .setCompressListener(new OnCompressListener() {
                                    @Override
                                    public void onStart() {
                                        // TODO 压缩开始前调用，可以在方法内启动 loading UI
                                    }

                                    @Override
                                    public void onSuccess(File file) {
                                        mPresenter.updateHeadImg(file.getPath());
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        // TODO 当压缩过程出现问题时调用
                                    }

                                }).launch();

                    }
                }
            }
        }

    }

    @OnClick({R.id.secondHandPublicsh_conten_fbfs,R.id.secondHandPublicsh_conten_qy_txt,R.id.Mystory_addStory_PhoneSsqy_txt, R.id.secondHandPublicsh_conten_jtdz_txt, R.id.secondHandPublicsh_conten_jg_txt, R.id.secondHandPublicsh_conten_lf_txt, R.id.secondHandPublicsh_conten_dpgs_txt, R.id.secondHandPublicsh_next_txt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.secondHandPublicsh_conten_qy_txt:
                //区域
                new MultiSelectPopWindow1.Builder(this)
                        .setConfirmListener(new MultiSelectPopWindow1.OnConfirmClickListener() {
                            @Override
                            public void onClick(MessageEvent messageEvent) {
                                //所在区域
                                secondHandPublicshContenQyTxt.setText(messageEvent.getRoom() + (messageEvent.getDrawing() != null ? messageEvent.getDrawing() : "") + (messageEvent.getToilet() != null ? messageEvent.getToilet() : ""));
                                housePayWayBeanPartent = houseArea.get(messageEvent.getRoomIndex());
                                housePayWayBeanChild = housePayWayBeanPartent.getChildren().get(messageEvent.getDrawingIndex());
                                secondHandSaveRequest.setAreaId(Long.parseLong(housePayWayBeanChild.getDictId()));
                            }
                        })
                        .setDataArray(houseArea)
                        .setCancel(ArmsUtils.getString(this, R.string.user_set_editinfo_cancel))
                        .setTitle(ArmsUtils.getString(this, R.string.dilaog_titile_xzszqy_txt))
                        .setConfirm(ArmsUtils.getString(this, R.string.sure_txt))
                        .build()
                        .show(secondHandPublicshContenQyTxt);
                break;
            case R.id.Mystory_addStory_PhoneSsqy_txt:
                //切换号码归属
                showPopuWindow();
                break;
            case R.id.secondHandPublicsh_conten_fbfs:
                //发布方式
                new MultiSelectPopWindow.Builder(this)
                        .setConfirmListener(new MultiSelectPopWindow.OnConfirmClickListener() {
                            @Override
                            public void onClick(MessageEvent messageEvent) {
                                if (messageEvent.getSelectData().equals("个人发布")) {
                                    secondHandPublicshContenDpgsLay.setVisibility(View.GONE);
                                    secondHandPublicshContenDpgsView.setVisibility(View.GONE);
                                    secondHandSaveRequest.setPublishWay(0);//个人发布
                                } else {
                                    secondHandPublicshContenDpgsLay.setVisibility(View.VISIBLE);
                                    secondHandSaveRequest.setPublishWay(1);//店铺发布
                                    secondHandPublicshContenDpgsView.setVisibility(View.VISIBLE);
                                }
                                secondHandPublicshContenFbfs.setText(messageEvent.getSelectData());
                            }
                        })
                        .setDataArray(Arrays.asList(getResources().getStringArray(R.array.secondhand_publish_way)))
                        .setCancel(ArmsUtils.getString(this, R.string.user_set_editinfo_cancel))
                        .setTitle(ArmsUtils.getString(this, R.string.second_hand_publicsh_xzfbfs))
                        .setConfirm(ArmsUtils.getString(this, R.string.sure_txt))
                        .build()
                        .show(secondHandPublicshContenFbfs);
                break;
            case R.id.secondHandPublicsh_conten_jtdz_txt:
                if (houseArea==null||housePayWayBeanPartent==null||housePayWayBeanChild==null)
                {
                    showMessage("请先选择区域");
                    return;
                }
                Intent intentMap = new Intent(this, MapSelectAddressActivity.class);
                intentMap.putExtra("selectedAreaName",housePayWayBeanChild.getName());
                intentMap.putExtra("lat",housePayWayBeanChild.getLat());
                intentMap.putExtra("lng",housePayWayBeanChild.getLgt());
                startActivityForResult(intentMap, 1004);
                //收货地址
                break;
            case R.id.secondHandPublicsh_conten_jg_txt:
                //价格设置
                mPresenter.popuwindSelectSetPrice(this, secondHandPublicshContenJgTxt, new SecondHandPublicshPresenter.priceSetCall() {
                    @Override
                    public void callBack(String qwjg, String rsj) {
                        secondHandSaveRequest.setBuyPrice(rsj);
                        secondHandSaveRequest.setSalePrice(qwjg);
                        secondHandPublicshContenJgTxt.setText(qwjg + "P");
                    }
                },secondHandSaveRequest);
                break;
            case R.id.secondHandPublicsh_conten_lf_txt:
                //分类
                mPresenter.popuwindSelectpublishType(this, secondHandPublicshContenLfTxt, msecondHandTreeTypeBeans, new SecondHandItemClickCall() {
                    @Override
                    public void call(SecondHandTreeTypeBean secondHandTreeTypeBean) {
                        secondHandPublicshContenLfTxt.setText(secondHandTreeTypeBean.getName());
                        secondHandSaveRequest.setSecondHandCategoryId(Long.parseLong(secondHandTreeTypeBean.getCategoryId()));
                    }
                });
                break;
            case R.id.secondHandPublicsh_conten_dpgs_txt:
                MemberCenterUserInfoBean memberCenterUserInfoBean = DataHelper.getDeviceData(this, AppConstant.user_memberInfo);
                mPresenter.popuwindSelectStore(this, secondHandPublicshContenDpgsTxt, memberCenterUserInfoBean, mresultBeans, new HousePublishDuanZuPublishPresenter.SelectStoreCallback() {
                    @Override
                    public void callBack(MyStoryBean.ResultBean resultBean) {
                        secondHandSaveRequest.setPublishWay(1);
                        secondHandSaveRequest.setUserStoreId(Long.parseLong(resultBean.getStoreId()));
                        secondHandPublicshContenDpgsTxt.setText(resultBean.getStoreName());
                    }
                });
                break;
            case R.id.secondHandPublicsh_next_txt:
                if (StringUtils.ViewisEnpty(secondHandPublicshContenBtTxt)) {
                    showMessage("请输入标题");
                    return;
                }
                secondHandSaveRequest.setTitle(secondHandPublicshContenBtTxt.getText().toString());
                if (images.size() < 2) {
                    showMessage("请选择照片");
                    return;
                }
                images.remove(images.size() - 1);
                secondHandSaveRequest.setImages(images);
                if (secondHandSaveRequest.getSecondHandCategoryId()==null||secondHandSaveRequest.getSecondHandCategoryId() < 1) {
                    showMessage("请选择物品分类");
                    return;
                }
                if (secondHandSaveRequest.getSalePrice() == null || TextUtils.isEmpty(secondHandSaveRequest.getSalePrice())) {
                    showMessage("请设置价格");
                    return;
                }
                if (secondHandSaveRequest.getLat() == null || secondHandSaveRequest.getLgt() == null || TextUtils.isEmpty(secondHandSaveRequest.getLat()) || TextUtils.isEmpty(secondHandSaveRequest.getLgt())) {
                    showMessage("请选择详细地址");
                    return;
                }
                if ( houseArea == null||secondHandSaveRequest.getAreaId()<1) {
                    showMessage("请选择所在区域");
                    return;
                }
                if (StringUtils.ViewisEnpty(secondHandPublicshContenFbfs))
                {
                    showMessage("请选择发布方式");
                    return;
                }

                if (MystoryAddStoryPhoneSsqyTxt.getText().toString().equals("+63")) {
                    if (MystoryAddStoryLxdh.getText().toString().substring(0, 1).equals("0")) {
                        showMessage("手机号不能是0开头");
                        return;
                    }
                    if (!MystoryAddStoryLxdh.getText().toString().substring(0, 1).equals("9")) {
                        showMessage("手机号必须是9开头");
                        return;
                    }
                    if (MystoryAddStoryLxdh.getText().toString().length() != 10) {
                        showMessage("手机号长度必须是十位");
                        return;
                    }
                } else {
                    if (MystoryAddStoryLxdh.getText().toString().length() != 11) {
                        showMessage("手机号必须是11位");
                        return;
                    }
                    if (!MystoryAddStoryLxdh.getText().toString().substring(0, 1).equals("1")) {
                        showMessage("手机号格式有误");
                        return;
                    }
                }
                secondHandSaveRequest.setLabelIds(mPresenter.getLabs(housePayWayBeansBq));
                secondHandSaveRequest.setMobile(MystoryAddStoryPhoneSsqyTxt.getText().toString().replace("+", "").trim() + MystoryAddStoryLxdh.getText().toString());
                secondHandSaveRequest.setWechat(housepublishpublishContenWxhTxt.getText().toString());
                secondHandSaveRequest.setDetail(secondHandPublicshContenMsTxt.getText().toString());
                if (getIntent().hasExtra("secondHandId")){
                    mPresenter.secondHandEdit(getIntent().getStringExtra("secondHandId"),secondHandSaveRequest);
                }else {
                    //添加
                    mPresenter.secondHandAdd(secondHandSaveRequest);
                }

                break;
        }
    }

    /**
     * 控制popu显示
     */
    public void showPopuWindow() {
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(this, R.layout.user_popuwindow_view)).parentView(MystoryAddStoryPhoneSsqyTxt).backgroundDrawable(getResources().getDrawable(R.drawable.button_white_bg))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isWrap(true).isHaveBackGround(true).build();
        View view1 = customPopupWindow.getContentView();
        TextView user_popuwindow_view_flb = view1.findViewById(R.id.user_popuwindow_view_flb);
        TextView user_popuwindow_view_zg = view1.findViewById(R.id.user_popuwindow_view_zg);
        user_popuwindow_view_flb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MystoryAddStoryPhoneSsqyTxt.setText("+63");
                customPopupWindow.dismiss();
            }
        });
        user_popuwindow_view_zg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MystoryAddStoryPhoneSsqyTxt.setText("+86");
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (requestCode == 1005 || requestCode == AppConstant.REQUEST_CODE_CHOOSE) {
                List<Uri> temSelected = Matisse.obtainResult(data);
                if (temSelected != null && !temSelected.isEmpty()) {
                    YsImg(temSelected);
                }
            }
            if (requestCode == 1004 && resultCode == RESULT_OK && data != null) {
                //选择地址返回
                if (!data.hasExtra("SelectAddressMapBean")) {
                    showMessage("选取地址异常");
                    return;
                }
                secondHandPublicshContenJtdzTxt.setFocusableInTouchMode(true);
                secondHandPublicshContenJtdzTxt.setFocusable(true);
                secondHandPublicshContenJtdzTxt.requestFocus();
                SelectAddressMapBean selectAddressMapBean = (SelectAddressMapBean) data.getSerializableExtra("SelectAddressMapBean");
                secondHandPublicshContenJtdzTxt.setText(selectAddressMapBean.getDetileAddress());
                secondHandSaveRequest.setDeliveryAddress(selectAddressMapBean.getDetileAddress());
                secondHandSaveRequest.setLgt(selectAddressMapBean.getLgt());
                secondHandSaveRequest.setLat(selectAddressMapBean.getLat());
            }

        }
    }
}
